package com.talking.activities

import android.annotation.SuppressLint
import android.content.Intent
import android.graphics.Typeface
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.google.gson.JsonParser
import com.parfoismeng.slidebacklib.registerSlideBack
import com.parfoismeng.slidebacklib.unregisterSlideBack
import com.talking.R
import com.talking.databinding.ActivityForgetPasswordBinding
import com.talking.util.Network
import com.talking.util.Tools
import com.talking.view.VerificationCodeView
import okhttp3.Call
import okhttp3.Callback
import okhttp3.Response
import java.io.IOException

class ForgetPasswordActivity : AppCompatActivity() {
    private val calls: MutableList<Call> = ArrayList()
    private var userYzm: String?=null
    private lateinit var binding: ActivityForgetPasswordBinding
    private var yzm: String? = null
    private var time = 40
    private var handle = Handler(Looper.getMainLooper())
    private var runnable: Runnable = object : Runnable {
        @SuppressLint("SetTextI18n")
        override fun run() {
            time--
            if (time <= 0) {
                binding.forgetSend.text="发送"
                time = 40
            } else {
                binding.forgetSend.text=time.toString() + "s后可发送"
                handle.postDelayed(this, 1000)
            }
        }
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityForgetPasswordBinding.inflate(layoutInflater)
        setContentView(binding.root)
        setResult(1)
        binding.forgetLogo.logo.typeface = Typeface.createFromAsset(assets, "font1.TTF")
        binding.forgetSend.setOnClickListener {
            if (binding.forgetSend.text.toString() == "发送"){
                yzm = Tools.createRandom()
                val map = HashMap<String, String>()
                map["mail"] = binding.forgetMail.text.toString()
                map["yzm"] = yzm.toString()
                Network.post(map, "user/sendEmail/") { call: Call ->
                    call.enqueue(object : Callback {
                        override fun onFailure(call: Call, e: IOException) { e.printStackTrace() }
                        override fun onResponse(call: Call, response: Response) { runOnUiThread { handle.postDelayed(runnable, 0) } }
                    })
                    calls.add(call)
                }
            }else Tools.toast(getString(R.string.please_wait_time))
        }
        binding.forgetPasswordVerify.setOnCodeFinishListener(object:VerificationCodeView.OnCodeFinishListener{
            override fun onTextChange(view:View?,content:String?){}
            override fun onComplete(view:View?,content:String?){userYzm=content}
        })
        binding.forgetChange.setOnClickListener {
            if (!yzm.equals(userYzm)){
                Tools.toast(getString(R.string.verify_code_error))
                return@setOnClickListener
            }
            if (binding.forgetPassword.text.toString() == ""){
                Tools.toast(getString(R.string.input_password))
                return@setOnClickListener
            }
            if (binding.forgetPassword2.text.toString() == ""){
                Tools.toast(getString(R.string.input_password_again))
                return@setOnClickListener
            }
            if (!Tools.passwordCorrect(binding.forgetPassword.text.toString())){
                Tools.toast(getString(R.string.password_format))
                return@setOnClickListener
            }
            if (binding.forgetPassword.text.toString() != binding.forgetPassword2.text.toString()){
                Tools.toast(getString(R.string.password_not_match))
                return@setOnClickListener
            }
            val map = HashMap<String, String>()
            map["mail"] = binding.forgetMail.text.toString()
            map["password"] = binding.forgetPassword.text.toString()
            Network.post(map, "user/changePasswordByMail/") { call: Call ->
                call.enqueue(object : Callback {
                    override fun onFailure(call: Call, e: IOException) { e.printStackTrace() }
                    override fun onResponse(call: Call, response: Response) { runOnUiThread {
                        val msg = JsonParser.parseString(response.body!!.string()).asJsonObject.get("msg").asString
                        if (msg.equals("修改成功")){
                            setResult(3, Intent().putExtra("mail",binding.forgetMail.text.toString()).putExtra("password",binding.forgetPassword.text.toString()))
                            finish()
                        }else Tools.toast(msg)
                    }}
                })
                calls.add(call)
            }
        }
        registerSlideBack { finish() }
    }
    override fun onDestroy() {
        super.onDestroy()
        unregisterSlideBack()
        for (c in calls)c.cancel()
    }
}